﻿using UnityEngine;
using System.Collections;

public class DeathDetection : MonoBehaviour, IResettable {

    public bool isBeingDestroyed = false;
    private bool doneBeingDestroyed = false;

    private RobotTouch robotTouch;
    private ShotSelectButton energyShotButton;
    private ShotSelectButton teleportShotButton;
    private ShotSelectButton missileShotButton;

    public GameObject deathEffect;
    private GameObject deathEffectInstance = null;

    public bool DoneBeingDestroyed { get { return doneBeingDestroyed; } }

	// Use this for initialization
	void Start () {

        robotTouch = transform.FindChild("RobotTouchReceiver").GetComponent<RobotTouch>();
        energyShotButton = GameObject.Find("UIElements").transform.FindChild("energyShotButton").GetComponent<ShotSelectButton>();
        teleportShotButton = GameObject.Find("UIElements").transform.FindChild("teleportShotButton").GetComponent<ShotSelectButton>();
        missileShotButton = GameObject.Find("UIElements").transform.FindChild("missileButton").GetComponent<ShotSelectButton>();

        ResetItemList.Instance.AddItem(this);

	}
	
	// Update is called once per frame
	void Update () {

        if (isBeingDestroyed)
        {
            if (!doneBeingDestroyed)
            {
                // Play the animation or whatever else happens while the player is being destroyed
                if (!deathEffectInstance.particleSystem.isPlaying)
                {
                    print("Done destroying the player");
                    doneBeingDestroyed = true;
                }
                else
                {
                    print("Still destroying the player");
                }
            }
            else
            {
                // Nothing for now; they can hit the Reset button
            }
        }

	}

    public void Electrocuted()
    {
        if (!isBeingDestroyed && !doneBeingDestroyed)
        {
            isBeingDestroyed = true;
            doneBeingDestroyed = false;

            robotTouch.TouchEnabled = false;
            energyShotButton.TouchEnabled = false;
            teleportShotButton.TouchEnabled = false;
            missileShotButton.TouchEnabled = false;

            deathEffectInstance = GameObject.Instantiate(deathEffect, transform.position, Quaternion.identity) as GameObject;
            this.renderer.enabled = false;
            this.collider.enabled = false;
        }


    }

    public void Reset()
    {
        isBeingDestroyed = false;
        doneBeingDestroyed = false;
        this.renderer.enabled = true;
        this.collider.enabled = true;
    }

    public bool PersistAfterReset()
    {
        return true;
    }
}
